import {
  TASK_BANNERS,
  TASK_LIST,
  TASK_SEARCH,
  SEARCH_TAG,
  TASK_CONTENT,
  BLANK_TASK
} from "../constants/task";
import { CONTENT_OF_TASK } from "../service/config";
import { getCache, setCache } from "../utils/cache";

const INITIAL_STATE = {
  tags: [],
  list: [],
  search_list: [],
  page: 1,
  search_page: 1,
  banners: [],
  taskContent: getCache(CONTENT_OF_TASK),
  nomore: false,
};

export default function records(state = INITIAL_STATE, action) {
  switch (action.type) {
    case TASK_BANNERS:
      return {
        ...state,
        banners: action.banners
      };
    case TASK_LIST:
      return {
        ...state,
        list:
          action.page === 1
            ? [...action.list]
            : [...state.list, ...action.list],
        page: action.page,
        nomore:action.list.length === 0
      };
    case SEARCH_TAG:
      return {
        ...state,
        tags: action.tags
      };
    case TASK_SEARCH:
      return {
        ...state,
        search_list:
          action.page === 1
            ? [...action.search_list]
            : [...state.search_list, ...action.search_list],
        search_page: action.page
      };
    case TASK_CONTENT:
      setCache(CONTENT_OF_TASK,action.taskContent)
      return {
        ...state,
        taskContent: action.taskContent
      };
    case BLANK_TASK:
      return {
        ...state,
        list: [],
        page:1
      }
    default:
      return { ...state };
  }
}
